<?php

namespace SmsSend;

use app\common\model\SmsLogs;

class SmsSend
{

    private $ip = ['101.251.214.153:8901'];

    private $userid = 'JI0981';                  //账户
    private $pwd = '785221';                    //密码
    private $isEncryptPwd = true;               //是否加密
    private $requestPath = '/sms/v2/std/';     //发送验证码的路径
    public $ERROR_310099 = -310099;            //http请求失败


       public function ch_send($tel = '', $msg = '')
    {
        if ($tel == false || !is_mobile($tel)) {
            return false;
        }
        $qiye_code = 'zhcfyzm';
        $user_id   = '90008';
        $pass      = 'zhcfyzm123';
        $auth      = md5($qiye_code.$pass);
        $data      = [
            'uid'    => $user_id,
            'auth'   => $auth,
            'mobile' => $tel,
            'msg'    => $msg,
            'expid'  => 9000802
        ];
        $sms       = SmsLogs::create(['mobile' => $tel, 'content' => $msg, 'source' => 1, 'type' => 1]);

        if(env('SMS_OPEN')) {
            $EncryptUtil = new EncryptUtil();
            $data['msg'] = urlencode($data['msg']);
            $data['msg'] = $EncryptUtil->encrypt_content($msg);//短信内容进行urlencode加密
            $data        = $post = http_build_query($data);//将数组转化为JSON格式
            $Chttp       = new Chttp();
            $result      = $Chttp->PostCURL('http://47.96.75.8:9011/hy/', $data);//进行请求
            list($status) = explode(',', $result);
            if ($status === '0') {
                $res = true;
            }else {
                $res = false;
            }
            $status = $status === '0' ? SmsLogs::SMS_STATUS_ING : SmsLogs::SMS_STATUS_ERROR;

        }else{
            $res = true;
            $status = SmsLogs::SMS_STATUS_CLOSE;
        }

        if (true == $res) {
            $sms->status = $status;
            $sms->save();
        }
        return $res;
    }

    // 口红机
    public function tanqu_send($tel = '', $verify_code = '')
    {
        if ($tel == false || !is_mobile($tel)) {
            return false;
        }
        $qiye_code = 'zhcfyzm';
        $user_id   = '90008';
        $pass      = 'zhcfyzm123';
        $auth      = md5($qiye_code.$pass);
        $data      = [
            'uid'    => $user_id,
            'auth'   => $auth,
            'mobile' => $tel,
            'tplCode'=> 239,
            'tplParam'=>json_encode([1=>$verify_code]),
//            'msg'    => $msg,
//            'expid'  => 9000816
        ];
        $sms       = SmsLogs::create(['mobile' => $tel, 'content' => '【探趣】验证码为'.$verify_code.'，5分钟内有效。任何人向您索要验证码均为诈骗，请勿泄露。', 'source' => 1, 'type' => 1]);

        if(env('SMS_OPEN')) {
//            $EncryptUtil = new EncryptUtil();
//            $data['msg'] = urlencode($data['msg']);
//            $data['msg'] = $EncryptUtil->encrypt_content($msg);//短信内容进行urlencode加密
            $data        = $post = http_build_query($data);//将数组转化为JSON格式
            $Chttp       = new Chttp();
            $result      = $Chttp->PostCURL('http://47.96.75.8:8386/smsmanage/hy/tp?'.$data, '');//进行请求
            list($status) = explode(',', $result);
            if ($status === '0') {
                $res = true;
            }else {
                $res = false;
            }
            $status = $status === '0' ? SmsLogs::SMS_STATUS_ING : SmsLogs::SMS_STATUS_ERROR;

        } else {
            $res = true;
            $status = SmsLogs::SMS_STATUS_CLOSE;
        }

        if (true == $res) {
            $sms->status = $status;
            $sms->save();
        }
        return $res;
    }

    //探趣发货通知
    public function ad_send($tel = '', $order_no = '')
    {
        if ($tel == false || !is_mobile($tel)) {
            return false;
        }
        $qiye_code = 'zhcfyzm';
        $user_id   = '90008';
        $pass      = 'zhcfyzm123';
        $auth      = md5($qiye_code.$pass);
        $data      = [
            'uid'    => $user_id,
            'auth'   => $auth,
            'mobile' => $tel,
            'tplCode'=> 240,
            'tplParam'=>json_encode([1=>$order_no]),
        ];
        if(env('SMS_OPEN')) {
            $data        = http_build_query($data);//将数组转化为JSON格式
            $Chttp       = new Chttp();
            $Chttp->PostCURL('http://47.96.75.8:8386/smsmanage/hy/tp?'.$data, '');//进行请求
        }
    }

}